package com.aiyige.page.my.message.notifymessage.sysmessage;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.api.ApiManager;
import com.aiyige.model.Page;
import com.aiyige.model.message.entity.Message;
import com.aiyige.model.message.view.SystemContent;
import com.aiyige.page.my.message.notifymessage.sysmessage.adapter.SystemMessageAdapter;
import com.aiyige.page.my.message.notifymessage.sysmessage.model.Operation;
import com.aiyige.page.my.message.notifymessage.sysmessage.model.SystemMessage;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.widget.CommonDataView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;

@Route(path = ARouterConfig.SysMessagePage)
public class SysMessagePage extends AppCompatActivity implements CommonDataView.RetrofitStub {

    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.cdv)
    CommonDataView cdv;
    SystemMessageAdapter systemMessageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_sys_message);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        titleTv.setText(R.string.system_notify_title);
        systemMessageAdapter = new SystemMessageAdapter();
        cdv.config(systemMessageAdapter, this);
        systemMessageAdapter.setCommonDataView(cdv);
        cdv.doRefreshRequest();
    }

    @OnClick({R.id.titleBackBtn})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.titleBackBtn:
                finish();
                break;
        }
    }

    @Override
    public Call<ResponseBody> call(long currentPage) {
        return ApiManager.getService().message(Message.TYPE_SYSTEM, 15, currentPage);
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        try {
            Page page = JSON.parseObject(respJson, Page.class);
            List<Message> messageList = new LinkedList<>();
            if (!TextUtils.isEmpty(page.getContent())) {
                messageList.addAll(JSON.parseArray(page.getContent(), Message.class));
            }

            for (Message message : messageList) {
                SystemContent record = null;
                try {
                    record = message.getContent().toJavaObject(SystemContent.class);
                } catch (Exception e) {

                }
                if (record == null) {
                    continue;
                }

                List<Operation> operationList = new LinkedList<>();
                if (!ListUtil.isEmpty(message.getOperations())) {
                    for (String item : message.getOperations()) {
                        operationList.add(Operation.newBuilder().operationType(item).build());
                    }
                }

                resultList.add(SystemMessage.newBuilder()
                        .id(message.getId())
                        .operationList(operationList)
                        .content(record.getDetail())
                        .title(record.getTitle())
                        .time(message.getCreateTime())
                        .build());
            }
            return new CommonDataView.HandleResult(page.isLast());
        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }
    }
}
